#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
#include "mailUtil.h"

#define MAX_SIZE 65535

char buf[MAX_SIZE+1];

void recv_mail()
{
    char send_buf[MAX_SIZE];
    const char* host_name = "pop.qq.com"; // TODO: Specify the mail server domain name
    const unsigned short port = 110; // POP3 server port
    const char* user = "1271783358@qq.com"; // TODO: Specify the user
    const char* pass = "uklsswipqfaagjij"; // TODO: Specify the password
    char dest_ip[16];
    int s_fd; // socket file descriptor
    struct hostent *host;
    struct in_addr **addr_list;
    int i = 0;
    int r_size;

    // Get IP from domain name
    if ((host = gethostbyname(host_name)) == NULL)
    {
        herror("gethostbyname");
        exit(EXIT_FAILURE);
    }

    addr_list = (struct in_addr **) host->h_addr_list;
    while (addr_list[i] != NULL)
        ++i;
    strcpy(dest_ip, inet_ntoa(*addr_list[i-1]));

    // TODO: Create a socket,return the file descriptor to s_fd, and establish a TCP connection to the POP3 server
    s_fd = socket(AF_INET,SOCK_STREAM,0);
    struct sockaddr_in servaddr;
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = swap16(port);
    bzero(servaddr.sin_zero,8);
    struct in_addr ip_des;
    ip_des.s_addr = inet_addr(dest_ip);
    servaddr.sin_addr = ip_des;

    if (connect(s_fd,(struct sockaddr *)&servaddr,sizeof(struct sockaddr_in))==-1){
        perror("error in connect");
        exit(EXIT_FAILURE);
    }
    // Print welcome message
    if ((r_size = recv(s_fd, buf, MAX_SIZE, 0)) == -1)
    {
        perror("recv");
        exit(EXIT_FAILURE);
    }
    buf[r_size] = '\0'; // Do not forget the null terminator
    printf("%s", buf);
    memset(buf,MAX_SIZE,0);
    memset(send_buf,MAX_SIZE,0);
    printf("-------------------------\n");
    // TODO: Send user and password and print server response
    const char* loginUser = strcat(strcat(strcpy(send_buf,"user "),user),"\r\n"); // TODO: Enter EHLO command here
    send(s_fd, loginUser, strlen(loginUser), 0);
    if ((r_size = recv(s_fd, buf, MAX_SIZE, 0)) == -1)
    {
        perror("error in receive EHLO message\n");
        exit(EXIT_FAILURE);
    }
    buf[r_size] = '\0'; // Do not forget the null terminator
    printf("%s", buf);
    printf("-------------------------\n");
    memset(buf,MAX_SIZE,0);
    memset(send_buf,MAX_SIZE,0);

    const char* loginerPwd = strcat(strcat(strcpy(send_buf,"pass "),pass),"\r\n"); // TODO: Enter EHLO command here
    send(s_fd, loginerPwd, strlen(loginerPwd), 0);
    if ((r_size = recv(s_fd, buf, MAX_SIZE, 0)) == -1)
    {
        perror("error in receive EHLO message\n");
        exit(EXIT_FAILURE);
    }
    buf[r_size] = '\0'; // Do not forget the null terminator
    printf("%s", buf);
    printf("-------------------------\n");
    memset(buf,MAX_SIZE,0);
    memset(send_buf,MAX_SIZE,0);
    // TODO: Send STAT command and print server response
    const char* stat = strcat(strcpy(send_buf,"stat"),"\r\n"); // TODO: Enter EHLO command here
    send(s_fd, stat, strlen(stat), 0);
    if ((r_size = recv(s_fd, buf, MAX_SIZE, 0)) == -1)
    {
        perror("error in receive EHLO message\n");
        exit(EXIT_FAILURE);
    }
    buf[r_size] = '\0'; // Do not forget the null terminator
    printf("%s", buf);
    printf("-------------------------\n");
    memset(buf,MAX_SIZE,0);
    memset(send_buf,MAX_SIZE,0);
    // TODO: Send LIST command and print server response
    const char* list = strcat(strcpy(send_buf,"list"),"\r\n"); // TODO: Enter EHLO command here
    send(s_fd, list, strlen(list), 0);
    if ((r_size = recv(s_fd, buf, MAX_SIZE, 0)) == -1)
    {
        perror("error in receive EHLO message\n");
        exit(EXIT_FAILURE);
    }
    buf[r_size] = '\0'; // Do not forget the null terminator
    printf("%s", buf);
    printf("-------------------------\n");
    memset(buf,MAX_SIZE,0);
    memset(send_buf,MAX_SIZE,0);
    // TODO: Retrieve the first mail and print its content
    const char* retrFirst = strcat(strcpy(send_buf,"retr 1"),"\r\n"); // TODO: Enter EHLO command here
    send(s_fd, retrFirst, strlen(retrFirst), 0);
    if ((r_size = recv(s_fd, buf, MAX_SIZE, 0)) == -1)
    {
        perror("error in receive EHLO message\n");
        exit(EXIT_FAILURE);
    }
    buf[r_size] = '\0'; // Do not forget the null terminator
    printf("%s", buf);
    printf("-------------------------\n");
    memset(buf,MAX_SIZE,0);
    memset(send_buf,MAX_SIZE,0);
    // TODO: Send QUIT command and print server response
    const char* quit = strcat(strcpy(send_buf,"quit"),"\r\n"); // TODO: Enter EHLO command here
    send(s_fd, quit, strlen(quit), 0);
    if ((r_size = recv(s_fd, buf, MAX_SIZE, 0)) == -1)
    {
        perror("error in receive EHLO message\n");
        exit(EXIT_FAILURE);
    }
    buf[r_size] = '\0'; // Do not forget the null terminator
    printf("%s", buf);
    printf("-------------------------\n");
    memset(buf,MAX_SIZE,0);
    memset(send_buf,MAX_SIZE,0);
    close(s_fd);
}

int main(int argc, char* argv[])
{
    recv_mail();
    exit(0);
}
